import {rankingStore} from '../../store/rankingStore'
import { recommendStore } from '../../store/recommendSong'
import {getReqDetailSong} from '../../service/music'
import { playStore } from '../../store/playState'

Page({
  data: {
    type: "ranking",
    key: "newRanking",
    songInfos: {},
    id: 0,
    type: ''
  },
  onLoad(options) {
    // 确定获取的数据
    // type: ranking ->榜单数据
    // type: recommend -> 推荐数据
    const type = options.type
    this.setData({ type })

    // 获取store中榜单的数据
    if(type==='ranking') {
      const key = options.key
      this.data.key = key
      rankingStore.onState(key,this.handleRanking)
    } else if(type==='recommend') {
      recommendStore.onState('recommendInfo',this.handleRanking)
    } else if(type==='menu') {
      const id = options.id
      this.data.id = id
      this.fetchDetailSong()
    }
  },
  handleRanking(value) {
    this.setData({songInfos: value})
    wx.setNavigationBarTitle({
      title: value.name
    })
  },
  async fetchDetailSong() {
    const res = await getReqDetailSong(this.data.id)
    this.setData({songInfos: res.playlist})
  },
  onSongTap() {
    playStore.setState('playSongs',this.data.songInfos.tracks)
  },
  onUnload() {
    if(this.data.type === 'ranking') {
      rankingStore.offState(this.data.key,this.handleRanking)
    } else if(this.data.type === 'recommend') {
      recommendStore.offState('recommendInfo',this.handleRanking)
    }
  }
})